#pragma once
#include <cstddef>
#include <cstdint>
#include <mutex>

#include "option/config.h"

namespace boltDB {
class BasicPageAllocator {
 public:
  virtual page_id_t AllocatePage() = 0;
  virtual bool DeallocatePage(page_id_t) = 0;
  virtual ~BasicPageAllocator() = default; // 添加虚析构函数
};
/*
  页面分配器支持的最大空闲页的数量是 (4kb-12b-4b)/4b 即1020个空闲页
  他是从一个freelist page中初始化得来的,其基本结构如下所示: 
  ------------------------------------------------------------------
  |   PageHeader   |free_page_count|page_id 1|page_id 2|page_id ...|
  -----------------------------------------------------------------
  |  页面头        |              freeList页的内容                  |

  页面头的内容参见 stroage/page.h
*/
class Unorded_PageAllocator :public BasicPageAllocator {
 public:
  static constexpr size_t MAX_DATA_SIZE = BOLTDB_PAGE_SIZE - PAGE_HEADER_SIZE;
  static constexpr size_t MAX_FREE_PAGE_COUNT =
      (BOLTDB_PAGE_SIZE - PAGE_HEADER_SIZE - 1) / sizeof(page_id_t);
  explicit Unorded_PageAllocator(uint16_t* pages_count, char* data);
  ~Unorded_PageAllocator(){
    free_pages_=nullptr;
  }
  page_id_t AllocatePage() override;

  bool DeallocatePage(page_id_t page_id) override;

  inline char* GetData() { return reinterpret_cast<char*>(free_page_count_); }

 private:
  page_id_t* free_pages_;
  uint16_t& free_page_count_;
  page_id_t* next_page_id_;
  std::mutex latch_;
};

}  // namespace boltDB